
//
// Copyright 2009 Arne Halvorsen (arne.halvorsen@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Project page: http://code.google.com/p/webfg/

//
// 091210 - aha42 - Created.
//

package org.aha.webfg;

import java.util.HashMap;
import java.util.Map;

import org.aha.mf4j.FlickrException;
import org.aha.mf4j.FlickrSession;
import org.aha.mf4j.response.FlickrObject;

/**
 * <p>
 *   Utility methods of use when parsing Flickr tags.
 * </p>
 * @author Arne Halvorsen (aha42)
 */
public final class TagUtil 
{
  private TagUtil(){} // Utility pattern dictates private constructor.
  
  /**
   * <p>
   *   Gets name / value of tags that identify in raw name and value by a '='.
   * </p>
   * @param fs Used to fetch photo info from Flickr.
   * @param id Id to photo to parse tags for.
   * @return {@link Map} where keys are names and values are tag values. 
   * @throws FlickrException If fails.
   */
  public static Map<String, String> getNameValueTags(FlickrSession fs,
    String id) throws FlickrException
  {
    return getNameValueTags(fs, id, null);
  }
  
  /**
   * <p>
   *   Gets name / value of tags that identify in raw name and value by a '='.
   * </p>
   * @param fs Used to fetch photo info from Flickr.
   * @param id Id to photo to parse tags for.
   * @return {@link Map} where keys are names and values are tag values. 
   * @throws FlickrException If fails.
   */
  public static Map<String, String> getNameValueTags(FlickrSession fs,
    String id, Map<String, String> tags) throws FlickrException
  {
    if (fs==null)
    {
      throw new NullPointerException("fc");
    }
    if (id==null)
    {
      throw new NullPointerException("id");
    }
    
    tags=(tags==null) ? new HashMap<String, String>() : tags;
    
    FlickrObject info=fs.createRequest("flickr.photos.getInfo")
      .setArg("photo_id", id).perform().getMandatoryChild("photo");
    
    //System.out.println("-----");
    //info.printHierarchy();
    //System.out.println("-----");
    
    FlickrObject tagsFo=info.getChild("tags");
    FlickrObject[] tagFos=(tagsFo==null) ? 
      FlickrObject.E_ARRAY : tagsFo.getChildren("tag");
    int n=tagFos.length;
    if (n>0)
    {
      for (int i=0; i<n; i++)
      {
        FlickrObject tag=tagFos[i];
        String raw=tag.getAttribute("raw");
        addNameValueTag(tags, raw);
      }
    }
    
    return tags;
  }
  
  private static void addNameValueTag(Map<String, String> tags, String tag)
  {
    tag=tag.trim();
    int len=tag.length();
    if (len>2)
    {
      int idx=tag.indexOf('=');
      if (idx>0 && idx<len-1)
      {
        String name=tag.substring(0, idx);
        String value=tag.substring(idx+1, len);
        tags.put(name, value);
      }
    }
  }
  
}
